package com.skyjing.toolsuitls.kotlin.tools

import android.annotation.SuppressLint
import android.content.Context
import android.widget.Toast
import com.skyjing.toolsuitls.java.tools.Utils

/**
 * Created by Administrator on 2018/12/25 16:18
 * ToolUitls
 * 主构造函数私有化
 **/
class SingleToast private constructor(context: Context) {
    private var mContext: Context? = null
    private var mToast: Toast? = null

    //线程安全的懒汉式
//    companion object {
//        private var instance : SingleToast? = null
//        get() {
//            if(field == null){
//                field = SingleToast()
//            }
//            return field
//        }
//        @Synchronized
//        fun get():SingleToast{
//            return instance!!
//        }
//    }

    //双重校验锁式
    companion object {
        @JvmStatic
        val instance: SingleToast by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            SingleToast(Utils.getApp())
        }
    }

    init {
        mContext = context
        @SuppressLint("ShowToast")
        mToast = Toast.makeText(mContext, "", Toast.LENGTH_LONG)
    }

    fun show(resId: Int) {
        show(mContext!!.getText(resId))
    }

    fun show(resId: Int, duration: Int) {
        show(mContext!!.getText(resId), duration)
    }

    fun show(strCharSequence: CharSequence, duration: Int = Toast.LENGTH_LONG) {
        mToast!!.duration = duration
        mToast!!.setText(strCharSequence)
        mToast!!.show()
    }

    fun show(strCharSequence: CharSequence, duration: Int = Toast.LENGTH_LONG, gravity: Int) {
        mToast!!.duration = duration
        mToast!!.setText(strCharSequence)
        mToast!!.setGravity(gravity, 0, 0)
        mToast!!.show()
    }

    fun show(resId: Int, duration: Int, gravity: Int) {
        show(mContext!!.getText(resId), duration, gravity)
    }

    fun show(strCharSequence: CharSequence, duration: Int, gravity: Int, x: Int, y: Int) {
        mToast!!.duration = duration
        mToast!!.setText(strCharSequence)
        mToast!!.setGravity(gravity, x, y)
        mToast!!.show()
    }

    fun show(resId: Int , duration: Int , gravity: Int ,x: Int ,y: Int){
        mToast!!.duration = duration
        mToast!!.setText(mContext!!.getText(resId))
        mToast!!.setGravity(gravity, x, y)
        mToast!!.show()
    }

    fun cancel() {
        mToast!!.cancel()
    }
}